import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMenu, QAction

from PyQt5.QtCore import QTimer


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.init_UI()

    def init_UI(self):
        self.resize(300, 300)
        self.setWindowTitle("普通按鈕")
        self.bt1 = QPushButton("这是什么", self)
        self.bt1.move(40, 40)

        self.bt2 = QPushButton("发送验证码", self)
        self.bt2.move(150, 40)

        self.mneu = QMenu(self)
        self.action1 = QAction("我是", self)

        action2 = QAction("世界上", self)
        action3 = QAction("最帅的", self)
        self.mneu.addAction(self.action1)
        self.mneu.addSeparator()
        self.mneu.addAction(action2)
        self.mneu.addSeparator()
        self.mneu.addAction(action3)

        self.bt1.setMenu(self.mneu)

        self.count = 10

        self.bt2.clicked.connect(self.Action)
        self.mneu.triggered.connect(self.changeValue)
        # self.bt1.clicked.connect(self.changeValue)

        self.time = QTimer(self)
        self.time.setInterval(1000)
        self.time.timeout.connect(self.Refresh)

        self.show()

    def changeValue(self):
        # actions = self.mneu.actions()
        # # 打印每个菜单项的文本
        # for action in actions:
        #     print(action.text())
        sender = self.mneu.sender()
        if sender == self.action1:
            print(self.action1.text())
        
   




        # sender = self.sender()
        # print(sender.text())

    def Action(self):
        if self.bt2.isEnabled():
            self.time.start()
            self.bt2.setEnabled(False)

    def Refresh(self):
        if self.count > 0:
            self.bt2.setText(str(self.count) + "秒后重发")
            self.count -= 1
        else:
            self.time.stop()
            self.bt2.setEnabled(True)
            self.bt2.setText("发送验证码")
            self.count = 10


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
